var meter;
var timeTrue = true;
var scriptObj = {
    /**debug按钮开关**/
    debug:function (value) {
        if (value == "on") {
            $("#debug").show();
            $("#debugButton").val("off");
            $("#debugButton").text("关闭调试");
            /**设置文件起始位置0**/
            $("#lastTimeFileSize").val(0);
            /**滚动条跳转到底部**/
            window.scrollTo(0, 99999);
            /**清除页面的日志内容,以及文件内容**/
            $("#logDiv").empty();
            scriptObj.clearLog();
            scriptObj.setScriptDebugStatus(true);
            /**定时器不断刷新调试日志内容刷出到页面上**/
            timeTrue = true;
            scriptObj.myrefresh();
        } else {
            $("#debug").hide();
            $("#debugButton").val("on");
            $("#debugButton").text("开启调试");
            scriptObj.setScriptDebugStatus(false);
            /**滚动条回到顶部**/
            window.scrollTo(0,0);
            /**关闭定时器**/
            clearTimeout(meter);
            timeTrue = false;
        }
    },
    /**设置debug状态**/
    setDebugStatus:function(interfaceMockId,value){
        var url = "/scriptController/updateDebugStatus";
        $.ajax({
            url: url,
            type: "post",
            data: {
                debug: value,
                interfaceMockId:interfaceMockId
            },
            success: function (result) {
                if (result.success) {
                    // alert("保存成功!");
                } else {
                    // alert(result.msg)
                }
            }
        })
    },
    /**设置debug状态**/
    setScriptDebugStatus: function (value) {
        var interfaceMockId = $("#interfaceMockId").val();
        scriptObj.setDebugStatus(interfaceMockId,value)
    },
    /**清除调试日志内容**/
    clearLog: function () {
        var url = "/scriptController/clearLog";
        var fileName = $("#interfaceAliasName").val() + $("#interfaceMockId").val();
        $.ajax({
            url: url,
            type: "post",
            data: {
                fileName: fileName
            },
            success: function (result) {
                if (result.success) {
                    // alert("保存成功!");
                } else {
                    // alert(result.msg)
                }
            }
        })
    },
    /**保存脚本**/
    save: function () {
        var url = "/scriptController/updateScript";
        var script = editor.getValue();
        var interfaceMockId = $("#interfaceMockId").val();
        var scriptId = $("#scriptId").val();
        $.ajax({
            url: url,
            type: "post",
            traditional: true,
            data: {
                script: script,
                id: scriptId,
                interfaceMockId: interfaceMockId
            },
            success: function (result) {
                if (result.success) {
                    $("#debugButton").show();
                    alert("保存成功!");
                } else {
                    alert(result.msg)
                }
            }
        })
    },
    /**获取调试log**/
    getDebugLog: function () {
        var url = "/scriptController/debugLog";
        var lastTimeFileSize = $("#lastTimeFileSize").val();
        var interfaceAliasName = $("#interfaceAliasName").val();
        var interfaceMockId = $("#interfaceMockId").val();
        var fileName = interfaceAliasName + interfaceMockId;
        var childrenObj = $("#reportLog").children();
        $.ajax({
            url: url,
            dataType: "json",
            async: false,
            type: "post",
            data: {
                fileName: fileName,
                lastTimeFileSize: lastTimeFileSize
            }, success: function (result) {
                if (null != result) {
                    $("#logDiv").append(result.strReport);
                    $("#lastTimeFileSize").val(result.lastTimeFileSize);
                    /**每次日志更新的时候滚动条自动下拉最下面**/
                    window.scrollTo(0, 99999);
                }
            }
        })
    },
    /**开启刷新定时器**/
    myrefresh: function () {
        /**调用获取日志方法,读取日志刷新到页面上**/
        scriptObj.getDebugLog();
        if (timeTrue) {
            /**第一参数自身的方法名,第二个时间单位毫秒**/
            meter = setTimeout('scriptObj.myrefresh()', 1000); //指定5秒刷新一次
        }
    },
    /**关闭页面**/
    custom_close: function () {
        if
        (confirm("您确定要关闭本页吗？")) {
            window.opener = null;
            window.open('', '_self');
            window.close();
        }
        else {
        }
    }
}